<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>餐桌修改</title>
    <link rel="stylesheet" href="../layui/css/layui.css"/>
    <script type="text/javascript" src="../layui/layui.js"></script>
</head>
<body>
<div class="container">
    <form class="layui-form" lay-filter="formTest" id="updateForm">
        <div class="layui-form-item" style="margin-top: 10px">
            <label class="layui-form-label">
                桌号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="table_id" name="id"
                       disabled autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                状态
            </label>
            <div class="layui-input-inline">
                <select id="selected" name="state">
                    <option value="空闲">空闲</option>
                    <option value="待结">待结</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" key="set-mine" lay-filter="save" lay-submit>
                保存
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var id = GetQueryString("tableId");
        $.ajax({
            url: "../findTableByTableId.do",
            data: {"tableId": id},
            success: function (data) {
                form.val("formTest", {
                    "id": data.tableId // "name": "value"
                    , "state": data.tableState
                })
            },
            dataType: "json",
            error: function () {
                layer.alert("系统出错");
            }
        })
        form.on('submit(save)', function (data) {
            var tableId = data.field.id;//表格数据
            var tableState = data.field.state;//表格数据
            $.post("../updateTable.do", {"tableId": tableId, "tableState": tableState}, function (data) {
                if (data.success) {
                    layer.alert(data.message, {icon: 6}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.layui.table.reload('DingTable');
                    });
                } else {
                    layer.alert(data.message, {icon: 3}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    });
                }
            }, "json");
            return false;
        });
    });

    //定义此方法可以获取到URL路径上传值
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");//获取参数值的正则表达式
        var urlKeys = window.location.search.substr(1);//获取url传参中？后面的值：id=123&name=小明
        urlKeys = decodeURI(urlKeys);//针对中文乱码的解决方案；decodeURI()解决乱码问题
        var r = urlKeys.match(reg);
        if (r != null) {
            return r[2];
        } else {
            return null;
        }
    }
</script>
</body>
</html>